/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.editor.textEditor;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;

public class PigScanner extends RuleBasedScanner {

    // list extracted from QueryParserConstants
    private final String[] keywords = { "ALL", "AND", "ANY", "ARRANGE", "AS",
            "BY", "COGROUP", "CONTINUOUSLY", "COUNT", "CROSS", "DISTINCT",
            "EVAL", "FILTER", "FILTEROP", "FLATTEN", "FOREACH", "GENERATE",
            "GROUP", "HOURS", "IDENTIFIER", "IF", "INNER", "INTO", "JOIN",
            "LOAD", "MATCHES", "MINUTES", "NOT", "OR", "ORDER", "OUTER",
            "PARALLEL", "SECONDS", "SPLIT", "STAR", "STORE", "TOKENIZE",
            "TUPLES", "UNION", "USING", "WINDOW" };

    private final ColorManager manager;

    public PigScanner(final ColorManager manager) {
        this.manager = manager;

        final IRule[] rules = new IRule[2];

        // Add generic whitespace rule.
        rules[0] = new WhitespaceRule(new PigWhitespaceDetector());

        // keywords rule
        final WordRule keywordRule = addWordRule(
                getCaseInsensitiveKeywords(keywords), getKeywordInstr());
        rules[1] = keywordRule;

        setRules(rules);
    }

    private WordRule addWordRule(final String[] words, final IToken token) {
        final WordRule wordRule = new WordRule(new KeywordDetector(),
                getDefaultInstr());
        for (int i = 0; i < words.length; i++) {
            wordRule.addWord(words[i], token);
        }
        return wordRule;
    }

    private IToken getDefaultInstr() {
        final IToken defaultInstr = new Token(new TextAttribute(manager
                .getColor(IPigColorConstants.DEFAULT)));
        return defaultInstr;
    }

    private IToken getKeywordInstr() {
        final IToken keywordInstr = new Token(new TextAttribute(manager
                .getColor(IPigColorConstants.KEYWORD)));
        return keywordInstr;
    }

    /**
     * Create an array of all keyword both in upper and lower case.
     * 
     * @param keywords
     * @return
     */
    private String[] getCaseInsensitiveKeywords(String[] keywords) {
        final String[] results = new String[keywords.length * 2];
        for (int i = 0; i < keywords.length; i++) {
            results[i] = keywords[i].toUpperCase();
        }
        int j = 0;
        for (int i = keywords.length; i < (keywords.length * 2); i++) {
            results[i] = keywords[j].toLowerCase();
            j++;
        }
        return results;
    }
}
